<template>
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="100px"
      >
        <el-form-item label="宗地序号" prop="id">
          <el-input
            v-model="queryParams.id"
            placeholder="请输入宗地序号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="宗地编号" prop="landNo">
          <el-input
            v-model="queryParams.landNo"
            placeholder="请输入宗地编号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="土地证号" prop="landCertificate">
          <el-input
            v-model="queryParams.landCertificate"
            placeholder="请输入土地证号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <!-- <el-form-item label="宗地坐落" prop="landLocation">
          <el-input
            v-model="queryParams.landLocation"
            placeholder="请输入宗地坐落"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item label="权利性质" prop="landQlxz">
          <el-input
            v-model="queryParams.landQlxz"
            placeholder="请输入权利性质"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="批准用途" prop="landApprovedUse">
          <el-input
            v-model="queryParams.landApprovedUse"
            placeholder="请输入批准用途"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="实际用途" prop="landActualUse">
          <el-input
            v-model="queryParams.landActualUse"
            placeholder="请输入实际用途"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item> -->
        <!-- <el-form-item label="土地级别" prop="landLevel">
        <el-input
          v-model="queryParams.landLevel"
          placeholder="请输入土地级别"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
        <el-form-item label="不动产单元号" prop="landBdcdyh">
          <el-input
            v-model="queryParams.landBdcdyh"
            placeholder="请输入不动产单元号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.SYS_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->

        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="primary"
            plain
            @click="openForm('create')"
            v-hasPermi="['zszx:land:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
          <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['zszx:land:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <!-- 子表的列表 -->
        <el-table-column type="expand">
          <template #default="scope">
            <el-tabs model-value="landFile">
              <el-tab-pane label="地块档案" name="landFile">
                <LandFileList :belong-id="scope.row.id" />
              </el-tab-pane>
              <el-tab-pane label="权利人信息" name="obligee">
                <ObligeeList :land-id="scope.row.id" />
              </el-tab-pane>
            </el-tabs>
          </template>
        </el-table-column>
        <el-table-column label="选择" align="center" width="80">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleChoose(scope.row)">确认</el-button>
          </template>
        </el-table-column>
        <el-table-column label="宗地序号" align="center" prop="id" width="60">
          <template #default="scope">
            <el-link type="primary" @click="openForm('update', scope.row.id)">{{
              scope.row.id
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="宗地编号" align="center" prop="landNo" width="200" />
        <el-table-column label="土地证号" align="center" prop="landCertificate" width="200" />
        <el-table-column label="宗地坐落" align="center" prop="landLocation" width="180" />
        <el-table-column label="面积" align="center" prop="landArea" />
        <el-table-column label="征收标准（元/平方米*年）" align="center" prop="levyStandards" />
        <el-table-column label="权利性质" align="center" prop="landQlxz" />
        <el-table-column label="批准用途" align="center" prop="landApprovedUse" />
        <el-table-column label="实际用途" align="center" prop="landActualUse" />
        <el-table-column label="土地级别" align="center" prop="landLevel" />
        <el-table-column label="不动产单元号" align="center" prop="landBdcdyh" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.SYS_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>

        <!-- <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-show="scope.row.landCoordinate"
            @click="handleAim(scope.row)"
          >
            定位
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['zszx:land:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zszx:land:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <LandForm ref="formRef" @success="getList" />

  <!-- 地图定位对话框-->
  <el-dialog title="定位查看" v-model="mapOpen" ref="mapDialog" width="1200px" append-to-body>
    <div style="position: relative; height: 80vh; border: 1px solid rgb(0 204 255)">
      <Map ref="arcgisMap" v-show="mapOpen" />
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { LandApi, LandVO } from '@/api/zszx/land'
import LandForm from '@/views/zszx/land/LandForm.vue'
import LandFileList from '@/views/zszx/land/components/LandFileList.vue'
import ObligeeList from '@/views/zszx/land/components/ObligeeList.vue'

import Map from '@/views/zszx/arcgis/Map.vue'
const { proxy } = getCurrentInstance()

/** 宗地信息 列表 */
defineOptions({ name: 'ChooseLand' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化


const loading = ref(true) // 列表的加载中
const list = ref<LandVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  landNo: undefined,
  landCertificate: undefined,
  landLocation: undefined,
  landDistrict: undefined,
  landTownship: undefined,
  landVillage: undefined,
  landArea: [],
  levyStandards: [],
  landQlxz: undefined,
  landApprovedUse: undefined,
  landActualUse: undefined,
  landLevel: undefined,
  landBdcdyh: [],
  status: undefined,
  creator: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const mapOpen = ref(false)

const emit = defineEmits(['landSelected']) // 定义 success 事件，用于操作成功后的回调

/** 确认选择宗地按钮 */
function handleChoose(row) {
  emit('landSelected', row)
}

// /** 打开弹窗 */
// const open = async () => {
//   dialogVisible.value = true
// }
// const close = async () => {
//   dialogVisible.value = false
// }
// defineExpose({ open,close }) // 提供 open 方法，用于打开弹窗

/** 定位按钮操作 */
function handleAim(row) {
  // console.log("---",row.landCoordinate)
  if (row.landCoordinate && row.landCoordinate.trim() != '') {
    mapOpen.value = true
    nextTick(() => {
      // console.log('定位中')
      proxy.$refs.arcgisMap.gotoExtent(row)
    })
  } else {
    proxy.$modal.msgError('地块无坐标，无法定位!')
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await LandApi.getLandPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await LandApi.deleteLand(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await LandApi.exportLand(queryParams)
    download.excel(data, '宗地信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
